package com.example.jussi.sqlitedatabasedemo;

import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    EditText id, name, age;
    EditText findID;
    TextView findResult;
    SQLiteDatabase database;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        id = (EditText) findViewById(R.id.id);
        name = (EditText) findViewById(R.id.name);
        age = (EditText) findViewById(R.id.age);
        findID = (EditText) findViewById(R.id.findID);
        findResult = (TextView) findViewById(R.id.findResult);
        MyDatabase myDatabase = new MyDatabase(MainActivity.this);
        database = myDatabase.getWritableDatabase();
    }

    public void find(View source){
        findResult.setText("");
        String sql = "select * from user where id = ?";
        String[] selectArgs = new String[]{
                findID.getText().toString()
        };
        if("".equals(selectArgs[0]) || selectArgs == null){
            sql = "select * from user where '1' = ?";
            selectArgs = new String[]{"1"};
        }
        Cursor cursor = database.rawQuery(sql, selectArgs);
        while (cursor.moveToNext()){
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            int age = cursor.getInt(cursor.getColumnIndex("age"));
            findResult.append("编号：" + id + "\t\t姓名：" + name + "\t\t年龄：" + age + "\n");
        }
    }

    public void insert(View source){
        String sql = "insert into user(id, name, age) values(?, ?, ?)";
        try {
            Object[] bindArgs = new Object[]{
                    Integer.parseInt(id.getText().toString()),
                    name.getText().toString(),
                    Integer.parseInt(age.getText().toString())
            };
            database.execSQL(sql, bindArgs);
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(MainActivity.this, "Please input ...." , Toast.LENGTH_SHORT).show();
        }
    }

    public void delete(View source){
        String sql = "delete from user where id = ?";
        try {
            Object[] bindArgs = new Object[]{
                    Integer.parseInt(id.getText().toString()),
            };
            database.execSQL(sql, bindArgs);
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(MainActivity.this, "Please input deleted id. " , Toast.LENGTH_SHORT).show();
        }
    }

    public void update(View source){
        String sql = "update user set name=? , age=?  where id=?";
        try {
            Object[] bindArgs = new Object[]{
                    name.getText().toString(),
                    Integer.parseInt(age.getText().toString()),
                    Integer.parseInt(id.getText().toString())
            };
            database.execSQL(sql, bindArgs);
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(MainActivity.this, "Please input ...." , Toast.LENGTH_SHORT).show();
        }
    }
}
